package com.cet.algorithm.二叉树.二叉树的最小深度;

/**
 * @program: algorithm
 * @description: 二叉树的最小深度
 * @author: 陈恩涛
 * @create: 2022-07-19 22:50
 **/
public class LC111 {

    public static void main(String[] args) {
        final TreeNode root = new TreeNode(1, null, new TreeNode(2, new TreeNode(3), new TreeNode(4, new TreeNode(5), new TreeNode(6))));
        System.out.println(new LC111().minDepth(root));
    }

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (root.left == null) {
            return rightDepth + 1;
        }
        if (root.right == null) {
            return leftDepth + 1;
        }
        // 左右结点都不为null
        return Math.min(leftDepth, rightDepth) + 1;
    }
}
